//
//  BHMePersonalHomeViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/19.
//  个人主页

import UIKit

class BHMePersonalHomeViewController: BHBaseViewController {

    lazy var headerView: BHMePersonalHeaderView = {
        let view = BHMePersonalHeaderView.loadFromXib()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: BHMePersonalHeaderView.height)
        view.delegate = self
        return view
    }()
    
    lazy var backScrollView: UIScrollView = {
        let view = UIScrollView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height:UIScreen.height))
        view.contentSize = CGSize(width: 0, height: BHMePersonalHeaderView.height+UIScreen.showHeight)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.delegate = self
        view.rebuild()
        return view
    }()
    lazy var childContaintScorllView: UIScrollView = {
        let view = UIScrollView.init()
        view.frame = CGRect(x: 0, y: BHMePersonalHeaderView.height, width: UIScreen.width, height: UIScreen.showHeight)
        view.isPagingEnabled = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.bounces = false
        view.rebuild()
        view.isScrollEnabled = false
        return view
    }()
    
    lazy var serviceManager: BHMeServiceManager = {
        let manager = BHMeServiceManager.init()
        return manager
    }()
    
    
    @objc var userid: String?
    @objc var comeFromType: Int = 0 // 1的时候说明是从聊天页面来的
    fileprivate var userModel: BHUserPageDataModel?
    
    var canHomeScrollFlag: Bool = true
    fileprivate var current: Int = 0
    fileprivate var childrenVC: BHPersonalChildViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.backgroundColor = UIColor.color_34CE98.withAlphaComponent(0.0)
        self.bh_navigationBar.whiteStyle = true
        self.bh_navigationBar.title_label.text = "主页"
        self.bh_navigationBar.title_label.alpha = 0.0
        
        self.bh_navigationBar.rightBtn.setImage(UIImage.init(named: "bh_me_personalHome_more"), for: .normal)
        
        self.view.addSubview(self.backScrollView)
        self.backScrollView.addSubview(self.childContaintScorllView)
        self.backScrollView.addSubview(self.headerView)
        
        self.configChild()
        if self.userid == nil {
            self.headerView.refreshUserForSelf()
        }else {
            if self.userid == BHMeUserInfoModel.shared.userId { // 自己
                self.headerView.refreshUserForSelf()
            }else {
                self.refreshUserInfoService()
                self.serviceManager.personalMainService(userId: self.userid)
                
                self.headerView.attensionBtn.addBtnAction(action: {[weak self] (_) in
                    self?.attentionOrNotService()
                }, for: .touchUpInside)
                
                self.headerView.messageBtn.addBtnAction(action: {[weak self] (_) in
                    if self?.comeFromType == 1 {
                        self?.navigationController?.popViewController(animated: true)
                    }else {
//                        BHToast.toast(text: String.serviceWaiteOpen)
                        
                        BHRouter.shared.bh_createSingleChat(userId: self?.userid ?? "",name: self?.userModel?.name)
                    }
                    
                }, for: .touchUpInside)
            }
        }
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            return .lightContent
        }
    }
    
    
    private func configChild() {
        for i in 0...2 {
            let vc = BHPersonalChildViewController.init()
            vc.childType = i
            vc.userid = self.userid
            self.addChild(vc)
            vc.view.frame = CGRect(x: CGFloat(i)*UIScreen.width, y: 0, width: UIScreen.width, height: self.childContaintScorllView.bh_height)
            self.childContaintScorllView.addSubview(vc.view)
            if i == 0 {
                self.childrenVC = vc
            }
            vc.canSuperScollingActBlock = { [weak self] in
                self?.canHomeScrollFlag = true
                self?.callChildReset()
            }
        }
        self.childContaintScorllView.contentSize = CGSize(width: UIScreen.width*CGFloat(4), height: 0)
        self.childContaintScorllView.alwaysBounceVertical = false;
    }

    func changeToIndex(_ index: Int) {
        self.current = index
        let canScroll = self.childrenVC.childCanScorll
        self.childrenVC = self.children[index] as? BHPersonalChildViewController
        self.childrenVC.childCanScorll = canScroll
        self.childContaintScorllView.setContentOffset(CGPoint(x: CGFloat(index)*UIScreen.width, y: 0), animated: false)
    }
    
    func callChildReset() {
        for vc in self.children {
            let child = vc as! BHPersonalChildViewController
            if child.collectionView.contentOffset.y != 0 {
                child.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
            }
        }
    }
 
}

extension BHMePersonalHomeViewController:  BHMePersonalHeaderViewDelegate {
    func didPersonalHomeChangeIndex(type: Int) {
        self.changeToIndex(type)
    }
}
extension BHMePersonalHomeViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if self.canHomeScrollFlag == false{
            scrollView.contentOffset.y = BHMePersonalHeaderView.maxUp
            
            self.bh_navigationBar.backgroundColor = UIColor.color_34CE98.withAlphaComponent(1)
        }else {
    
            if scrollView.contentOffset.y >= (BHMePersonalHeaderView.maxUp) {
                scrollView.contentOffset.y = (BHMePersonalHeaderView.maxUp)
                self.canHomeScrollFlag = false
                self.childrenVC.childCanScorll = true

                self.bh_navigationBar.backgroundColor = UIColor.color_34CE98.withAlphaComponent(1)
            }else {
                if scrollView.contentOffset.y < 0 {
                    self.bh_navigationBar.backgroundColor = UIColor.color_34CE98.withAlphaComponent(0.0)
                }else {
                    let per = scrollView.contentOffset.y / BHMePersonalHeaderView.maxUp
                    self.bh_navigationBar.backgroundColor = UIColor.color_34CE98.withAlphaComponent(per)
                    self.bh_navigationBar.title_label.alpha = per
                }
            }
        }
    
    }
    
}

extension BHMePersonalHomeViewController {
    func refreshUserInfoService() {
        self.serviceManager.responseUserInfoBlock = { model in
            self.userModel = model
            self.headerView.refreshOther(model: model)
        }
    }
    
    func attentionOrNotService()  {
        if self.userModel == nil {
            return
        }
        SVProgressHUD.show()
        BHCommonService.attentionOrNotService(type: 5, businessId: self.userid ?? "") { (success, errorMsg) in
            SVProgressHUD.dismiss()
            if success == true {
                if self.userModel!.attentionFlag == 1 {
                    BHToast.toast(text: "关注已取消")
                    self.userModel!.attentionFlag = 0
                }else {
                    BHToast.toast(text: "已关注")
                    self.userModel!.attentionFlag = 1
                }
                self.headerView.refreshOther(model: self.userModel!)
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
}
